package com.lixin.smartclassroom.ui.home.tab2.activity

import android.graphics.Color
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.drake.channel.sendEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.getcustomerdetailbycode
import com.lixin.smartclassroom.api.setquanxian
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.ActivitySelectUserBinding
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter

/**
 * Description 选择用户
 * Author: lwc
 * Date:2023/12/19 14:07
 */
class SelectUserPermissionAct : BaseActivity<ActivitySelectUserBinding>() {
    override fun getViewBinding() = ActivitySelectUserBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "选择用户"
    private lateinit var listBean: ArrayList<BaseBean>
    override fun textMenuContent()="确定"
    private var customerid=""
    private var customerName=""
    private var courseid = ""
    private var taskType = ""
    override fun onTextMenuListener() {
        for(i in 0 until listBean.size){
            if(customerid.isEmpty()){
                customerid = listBean[i].id.toString()
            }else{
                customerid += ","+listBean[i].id.toString()
            }

            if(customerName.isEmpty()){
                customerName = listBean[i].name.toString()
            }else{
                customerName += ","+listBean[i].name.toString()
            }
        }
        intent.putExtra("customerName",customerName)
        intent.putExtra("customerid",customerid)
        intent.putExtra("courseid",courseid)
        intent.putExtra("type",taskType)
//        sendEvent(customerName,"selectUserName")
//        sendEvent(customerid,"selectUserId")
        setResult(RESULT_OK,intent)
        finish()
    }

    override fun iniView() {
        courseid = getIntentString("courseid")
        taskType = getIntentString("type")
        binding.apply {
            listBean = ArrayList()
            flowLayout.adapter = object : TagAdapter<BaseBean>(listBean) {
                override fun getView(parent: FlowLayout?, position: Int, t: BaseBean?): View {
                    val tv =
                        View.inflate(this@SelectUserPermissionAct, R.layout.flow_select_user, null)
                    val textView = tv.findViewById<TextView>(R.id.tv_name)
                    textView.text = t?.name
                    val rl = tv.findViewById<LinearLayout>(R.id.rl)
                    val imgDelete = tv.findViewById<ImageView>(R.id.imgDelete)
                    rl.setRoundRectBg(Color.parseColor("#FAF9F9"), dp2px(14f).toInt())
                    imgDelete.onClick {
                        listBean.removeAt(position)
                        notifyDataChanged()
                    }
                    return rl
                }
            }
        }
    }


    override fun onClick() {
        binding.apply {
            btnAdd.onClick {
                if (etHuDongCode.text().isEmpty()) {
                    toast("请输入邀请码")
                    return@onClick
                } else {
                    scopeNetLife {
                        val baseBean = getcustomerdetailbycode(etHuDongCode.text())
                        if(baseBean.result=="0"){
                            listBean.add(baseBean)
                            flowLayout.adapter.notifyDataChanged()
                        }else{
                            toast(baseBean.resultNote.toString())
                        }

                    }
                }
            }
        }
    }

}